{% extends "base.html" %}
{% block title %}{{title}}{% endblock %}
{% block h2 %}{{title}}{% endblock %}
{% block content %}
<script src="{{ url_for('static', filename='js/install.js') }}"></script>
<script src="{{ url_for('static', filename='js/waf.js') }}"></script>
{% if manage_rules == '1' %}
	{% from 'include/input_macros.html' import input, checkbox %}
	<table class="overview" id="waf_rules">
		<tr class="overviewHead">
			<td class="padding10 first-collumn">
				{{lang.words.rule_name}}
			</td>
			<td style="width: 100px;">{{lang.words.enabled|title()}}</td>
			<td>{{lang.words.desc|title()}}</td>
			<td></td>
		</tr>
		{% for r in rules %}
		<tr class="{{ loop.cycle('odd', 'even') }}" id="rule-{{r.id}}">
			<td class="padding10 first-collumn">{{r.rule_name}}</td>
			<td class="checkbox">
				{% set id = 'rule_id-' + r.id|string() %}
				{% if r.en == 1 %}
					{{ checkbox(id, checked='checked') }}
				{% else %}
					{{ checkbox(id) }}
				{% endif %}
			</td>
			<td style="padding-top: 5px;padding-bottom: 10px;">{{r.desc}}</td>
			<td style="padding: 0 10px 0 10px;">
				<a href="/app/waf/{{service}}/{{serv}}/rule/{{r.id}}" class="ui-button ui-widget ui-corner-all" title="{{lang.words.view|title()}} {{lang.words.this4}} {{lang.words.rule}}">{{lang.words.view|title()}}/{{lang.words.edit|title()}}</a>
			</td>
		</tr>
		{% endfor %}
	</table>
	<p><span class="add-button" title="{{lang.words.add|title()}} {{lang.words.w_a}} {{lang.words.new}} WAF {{lang.words.rule}}" onclick="addNewConfig()" style="margin-right: 20px;">+ {{lang.words.add|title()}}</span></p><br><br>
	<div id="add-new-config" style="display: none">
		<table class="overview">
			{% include 'include/tr_validate_tips.html' %}
			<tr>
				<td class="padding20">{{lang.words.rule_name}}:<span class="need-field">*</span></td>
				<td>{{ input('new_rule_name', type='text', placeholder='New protected rule', title='New protected rule') }}</td>
			</tr>
			<tr>
				<td class="padding20">{{lang.words.desc|title()}}:<span class="need-field">*</span></td>
				<td>{{ input('new_rule_description', type='text', placeholder='This rule protects again attacks', title='This rule protects again attacks') }}</td>
			</tr>
		</table>
	</div>
{% elif waf_rule_file %}
	<div class="left-space">
		<link rel="stylesheet" href="/inc/codemirror/lib/codemirror.css">
		<link rel="stylesheet" href="/inc/codemirror/addon/dialog/dialog.css">
		<link rel="stylesheet" href="/inc/codemirror/addon/fold/foldgutter.css">
		<script src="/inc/codemirror/lib/codemirror.js"></script>
		<script src="/inc/codemirror/addon/search/search.js"></script>
		<script src="/inc/codemirror/addon/search/searchcursor.js"></script>
		<script src="/inc/codemirror/addon/search/jump-to-line.js"></script>
		<script src="/inc/codemirror/addon/search/matchesonscrollbar.js"></script>
		<script src="/inc/codemirror/addon/search/match-highlighter.js"></script>
		<script src="/inc/codemirror/addon/dialog/dialog.js"></script>
		<script src="/inc/codemirror/addon/edit/matchbrackets.js"></script>
		<script src="/inc/codemirror/addon/edit/closebrackets.js"></script>
		<script src="/inc/codemirror/addon/comment/comment.js"></script>
		<script src="/inc/codemirror/addon/wrap/hardwrap.js"></script>
		<script src="/inc/codemirror/addon/fold/foldcode.js"></script>
		<script src="/inc/codemirror/addon/fold/foldgutter.js"></script>
		<script src="/inc/codemirror/addon/fold/brace-fold.js"></script>
		<script src="/inc/codemirror/addon/fold/comment-fold.js"></script>
		<script src="/inc/codemirror/addon/scroll/annotatescrollbar.js"></script>
		<script src="/inc/codemirror/mode/nginx.js"></script>
		<script src="/inc/codemirror/mode/haproxy.js"></script>
		<script src="/inc/codemirror/keymap/sublime.js"></script>
		<script src="/app/static/js/configshow.js"></script>
			<h4>{{lang.words.config|title()}} {{waf_rule_file}} {{lang.words.from|title()}} {{ serv }}</h4>
			<form action="/app/waf/{{service}}/{{serv}}/rule/{{waf_rule_id}}/save" name="saveconfig" id="saveconfig" method="post">
				<input type="hidden" value="{{ serv }}" name="serv">
				<input type="hidden" value="{{ cfg }}.old" name="oldconfig">
				<input type="hidden" value="{{ token }}" name="token">
				<input type="hidden" value="waf_{{ service }}" name="service">
				<input type="hidden" value="{{ config_file_name }}" name="config_file_name">
				<div style="width: 95%;">
					<textarea name="config" id="config_text_area" class="config" rows="35" cols="100">{{ config }}</textarea>
				</div>
				<p>
					<a href="/app/waf/{{service}}/{{serv}}/rules" class="ui-button ui-widget ui-corner-all" title="Return to rules management">{{lang.words.back|title()}}</a>
					{% if g.user_params['role'] <= 3 %}
					<button type="submit" value="save" name="save" class="btn btn-default" title="{{lang.words.save|title()}} {{lang.words.without}} {{lang.words.reloading}}">{{lang.words.save|title()}}</button>
					{% if service == 'haproxy' %}
					<button type="submit" value="" name="" class="btn btn-default">{{lang.phrases.save_and_restart}}</button>
					{% elif service == 'nginx' %}
					<button type="submit" value="" name="reload" class="btn btn-default">{{lang.phrases.save_and_reload}}</button>
					{% endif %}
					{% endif %}
				</p>
			</form>
	</div>
		<style>
		.CodeMirror {
			line-height: 1.2em;
			height: 70%;
		}
		</style>
		<script>
			var myCodeMirror = CodeMirror.fromTextArea(document.getElementById("config_text_area"),
				{
					mode: "modsec",
					lineNumbers: true,
					lineWrapping: true,
					autocapitalize: true,
					autocorrect: true,
					spellcheck: true,
					autoCloseBrackets: true,
					keyMap: "sublime",
					matchBrackets: true,
					foldGutter: true,
					showCursorWhenSelecting: true,
    				gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter", "breakpoints"],
					highlightSelectionMatches: {showToken: /\w/, annotateScrollbar: true}
				});
			myCodeMirror.refresh();
			myCodeMirror.on("beforeChange", function (cm, change) {
				$(window).bind('beforeunload', function(){
				  return 'Are you sure you want to leave?';
				});
			});
		</script>
{% else %}
	<style>
	.alert-danger {
		width: 450px;
		display: inline-block;
		height: 25px;
		padding-top: 0px;
	}
	</style>
	<script>
		$("#secIntervals").css("display", "none");
		var ip = [];
		var hostnamea = [];
		{% for s in servers_all %}
			{%- if service == 'haproxy' -%}
				{%- set service_in_table = s.15 -%}
			{%- else -%}
				{% set service_in_table = s.14 -%}
			{%- endif -%}
		{%- if service_in_table|int() == 1 -%}
			ip.push("{{ s[2] }}");
			var host = "{{s[2]}}";
			host = host.replace(/\./g, '\\.');
			hostnamea.push(host);
		{% endif %}
		{% endfor %}
	</script>
	{% if servers_all|length == 0 %}
		{% include 'include/getstarted.html' %}
	{% else %}
	<table class="overview">
		<tr class="overviewHead">
			<td class="padding10 first-collumn">{{lang.words.server|title()}}</td>
			<td>{{lang.words.actions|title()}}</td>
			<td>WAF {{lang.words.mode}}</td>
			{% if service == 'haproxy' %}
			<td>{{lang.words.metrics|title()}}</td>
			{% endif %}
			<td>{{lang.words.manage|title()}} {{lang.words.rules}}</td>
			<td>{{lang.words.log|title()}}</td>
			<td>
				<a onclick="showOverviewWaf(ip, hostnamea)" title="{{lang.words.refresh|title()}}" style="float: right; margin-right: 25px;">
					<span class="refresh"></span>
				</a>
			</td>
		</tr>
		{% for s in servers_all %}
			<tr class="{{ loop.cycle('odd', 'even') }}" id="{{s[2]}}"></tr>
		{% endfor %}
	</table>
	{% if service == 'haproxy' %}
	<link href="{{ url_for('static', filename='css/chart.min.css') }}" rel="stylesheet">
	<script src="/app/static/js/overview.js"></script>
	<script src="/app/static/js/metrics.js"></script>
	<script src="/app/static/js/chart.min-4.3.0.js"></script>
	<div id="table_metrics"></div>
	<div class="metrics-time-range">
		<b>{{lang.words.time_range|title()}}:</b>
		<select title="Choose time range" id="time-range">
			<option value="30">30 {{lang.words.minutes}}</option>
			<option value="60">1 {{lang.words.hour}}</option>
			<option value="180">3 {{lang.words.hours}}</option>
			<option value="360">6 {{lang.words.hours2}}</option>
			<option value="720">12 {{lang.words.hours2}}</option>
		</select>
	</div>
	<div style="clear: both;"></div>
	{% for s in servers %}
	<div class="chart-container">
		<canvas id="waf_{{s.server.ip}}" role="img"></canvas>
	</div>
	{% endfor %}
	{% endif %}
	<div id="dialog-confirm" style="display: none;">
		<p><span class="ui-icon ui-icon-alert" style="float:left; margin:3px 12px 20px 0;"></span>Are you sure?</p>
	</div>
	{% for s in servers %}
		{{s.ip}}
	{% endfor %}
	<script>
		{%- if service == 'haproxy' %}
		function showWafMetrics() {
			{%- for s in servers %}
				getWafChartData('{{s.server.ip}}');
			{%- endfor %}
		}
		showWafMetrics();
		{%- endif %}
		showOverviewWaf(ip, hostnamea);
		$( function() {
			$("#time-range").on('selectmenuchange', function () {
				removeData()
				let metrics = new Promise(
					(resolve, reject) => {
						{%- for s in servers %}
							getWafChartData('{{s.server.ip}}')
						{%- endfor %}
					});
				metrics.then();
			});
		});
	</script>
	<div class="add-note addName alert-info" style="width: inherit; margin-right: 15px; clear: both;">
		{{lang.words.read|title()}} {{lang.words.desc}} <a href="https://roxy-wi.org/description/waf" class="link" title="WAF" target="_blank">{{lang.words.here}}</a>
	</div>
	{% endif %}
{% endif %}
{% endblock %}
